﻿using SD.ContentSystem.AppService.Maps;
using SD.ContentSystem.Domain.Entities;
using SD.ContentSystem.Domain.IRepositories;
using SD.ContentSystem.Domain.Mediators;
using SD.ContentSystem.IAppService.DTOs.Outputs;
using SD.ContentSystem.IAppService.Interfaces;
using SD.IdentitySystem;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.Global.Transaction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace SD.ContentSystem.AppService.Implements
{
    /// <summary>
    /// 消息服务契约实现
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MessageContract : IMessageContract
    {
        #region # 字段及依赖注入构造器

        /// <summary>
        /// 领域服务中介者
        /// </summary>
        private readonly DomainServiceMediator _svcMediator;

        /// <summary>
        /// 仓储中介者
        /// </summary>
        private readonly RepositoryMediator _repMediator;

        /// <summary>
        /// 单元事务
        /// </summary>
        private readonly IUnitOfWorkCMS _unitOfWork;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        /// <param name="svcMediator">领域服务中介者</param>
        /// <param name="repMediator">仓储中介者</param>
        /// <param name="unitOfWork">单元事务</param>
        public MessageContract(DomainServiceMediator svcMediator, RepositoryMediator repMediator,
            IUnitOfWorkCMS unitOfWork)
        {
            this._svcMediator = svcMediator;
            this._repMediator = repMediator;
            this._unitOfWork = unitOfWork;
        }

        #endregion


        //命令部分

        #region # 创建消息 —— void CreateMessage(string title, string content...
        /// <summary>
        /// 创建消息
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="contact">联系人</param>
        /// <param name="phoneNo">联系电话</param>
        /// <param name="email">电子邮箱</param>
        public void CreateMessage(string title, string content, string contact, string phoneNo, string email)
        {
            Message message = new Message(title, content, contact, phoneNo, email);

            this._unitOfWork.RegisterAdd(message);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 回复消息 —— void ReplyMessage(Guid messageId, string feedBack)
        /// <summary>
        /// 回复消息
        /// </summary>
        /// <param name="messageId">消息Id</param>
        /// <param name="feedBack">回复内容</param>
        public void ReplyMessage(Guid messageId, string feedBack)
        {
            Message currentMessage = this._unitOfWork.Resolve<Message>(messageId);

            //获取当前用户
            string replyer = Membership.LoginInfo == null ? null : Membership.LoginInfo.LoginId;

            currentMessage.Reply(feedBack, replyer);

            this._unitOfWork.RegisterSave(currentMessage);
            this._unitOfWork.UnitedCommit();
        }
        #endregion


        //查询部分

        #region # 获取消息 —— MessageInfo GetMessage(Guid messageId)
        /// <summary>
        /// 获取消息
        /// </summary>
        /// <param name="messageId">消息Id</param>
        /// <returns>消息</returns>
        public MessageInfo GetMessage(Guid messageId)
        {
            Message currentMessage = this._repMediator.MessageRep.Single(messageId);
            MessageInfo currentMessageInfo = currentMessage.ToDTO();

            return currentMessageInfo;
        }
        #endregion

        #region # 分页获取消息列表 —— PageModel<MessageInfo> GetMessageByPage(string keywords...
        /// <summary>
        /// 分页获取消息列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>消息列表</returns>
        public PageModel<MessageInfo> GetMessageByPage(string keywords, int pageIndex, int pageSize)
        {
            int rowCount, pageCount;

            IEnumerable<Message> messages = this._repMediator.MessageRep.FindByPage(keywords, pageIndex, pageSize, out rowCount, out pageCount);
            IEnumerable<MessageInfo> messageInfos = messages.Select(x => x.ToDTO());

            return new PageModel<MessageInfo>(messageInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion
    }
}
